﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Entities;

namespace Data.DataBase
{
    public class MateriaAdapter : Data.Database.Adapter
    {
        private static List<Business.Entities.Materia> _Materias;

        public static List<Business.Entities.Materia> Materias
        {
            get 
            { 
                if(_Materias == null)
                {
                    _Materias = new List<Business.Entities.Materia>();
                    Business.Entities.Materia mat;

                    mat = new Business.Entities.Materia();
                    mat.Id = 1;
                    mat.IdPlan = 1;// En verdad aca debieria buscar a traves de PlanAdapter la materia que tiene id de plan.
                    mat.Descripcion = "Comunicaciones";
                    mat.HsSemanales = 4;
                    mat.HsTotales = 60;
                    mat.State = Business.Entities.BusinessEntity.States.Unmodified;

                    _Materias.Add(mat);

                    mat = new Business.Entities.Materia();
                    mat.Id = 2;
                    mat.IdPlan = 1;
                    mat.Descripcion = "Fisica II";
                    mat.HsSemanales = 5;
                    mat.HsTotales = 70;
                    mat.State = Business.Entities.BusinessEntity.States.Unmodified;

                    _Materias.Add(mat);
                }
                return _Materias;
            }
           
        }

        public Business.Entities.Materia GetOne(int id)
        {
            return Materias.Find(delegate(Business.Entities.Materia m) { return m.Id == id; });
        }

        public List<Materia> GetAll()
        {
            return new List<Materia>(Materias);
        }

        public void Save(Business.Entities.Materia materia)
        {
            switch (materia.State)
            {
                case BusinessEntity.States.New:
                    int nuevoId = 0;
                    // esta busqueda podria reemplazarse por un OrderBy que orden por ID.
                    foreach (Materia per in Materias)
                    {
                        if (per.Id > nuevoId)
                        {
                            nuevoId = per.Id;
                        }
                    }
                    materia.Id = nuevoId + 1;
                    Materias.Add(materia);
                    break;
                case BusinessEntity.States.Modified:
                    Materias[Materias.FindIndex(delegate(Materia m) { return m.Id == materia.Id; })] = materia;
                    break;
                case BusinessEntity.States.Deleted:
                    this.Delete(materia);
                    break;
            }
            materia.State = BusinessEntity.States.Unmodified;
        }

        public void Delete(Business.Entities.Materia materia)
        {
            _Materias.Remove(materia);
        }

        public void Delete(int id)
        {
            this.Delete(this.GetOne(id));
        }
    }
}
